package com.itheima.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.ulits.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ：zengjie
 * @date ：2020/11/20
 */
@Transactional
public class ClearExpiredOrder {

    @Autowired
    private OrderSettingDao orderSettingDao;


    public void findExpiredByToDay() throws Exception {
        System.out.println("我好帅");
        //获得当前月份最后一天
        String date2String = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());

        System.out.println(date2String);
        //获取所有过时的预约数据
        List<OrderSetting> orderSettingList = orderSettingDao.findExpiredByToDay(date2String);
        //循环插入备份表
        for (OrderSetting orderSetting : orderSettingList) {
            Date orderDate = orderSetting.getOrderDate();
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            orderSettingDao.orderSettingBackup(orderDate,number,reservations);
        }
        //删除过时数据
        //int i = 1/0;

       for (OrderSetting orderSetting : orderSettingList) {
            String orderDate = DateUtils.parseDate2String(orderSetting.getOrderDate());
           //String orderDate = orderSetting.getOrderDate();
            orderSettingDao.delectExpired(orderDate);
        }


    }
}
